package com.ruoyi.member.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.MemberInfo;
import com.ruoyi.db.mapper.MemberExtInfoMapper;
import com.ruoyi.db.mapper.MemberInfoMapper;
import com.ruoyi.db.model.excle.MemberInfoExcel;
import com.ruoyi.db.model.param.MemberInfoParam;
import com.ruoyi.market.utils.SysNumberUtil;
import org.springframework.stereotype.Service;
import com.ruoyi.member.service.IMemberInfoService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 用户列表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-02
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class MemberInfoServiceImpl implements IMemberInfoService {
    @Resource
    private MemberInfoMapper memberInfoMapper;
    @Resource
    private MemberExtInfoMapper memberExtInfoMapper;

    /**
     * 查询用户列表
     *
     * @param uid 用户列表主键
     * @return 用户列表
     */
    @Override
    public MemberInfo selectMemberInfoByUid(String uid) {
        return memberInfoMapper.selectMemberInfoByUid(uid);
    }

    /**
     * 查询用户列表列表
     *
     * @return 用户列表
     */
    @Override
    public List<MemberInfoExcel> selectMemberInfoList(MemberInfoParam memberInfoParam) {
        return memberInfoMapper.selectMemberInfoList(memberInfoParam);
    }

    /**
     * 查询用户列表列表
     *
     * @return 用户列表
     */
    @Override
    public List<Map<String, Object>> selectMemberInfoListMap(MemberInfoParam memberInfoParam) {
        return memberInfoMapper.selectMemberInfoListMap(memberInfoParam);
    }

    /**
     * 新增用户列表
     *
     * @param memberInfo 用户列表
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo) {
        memberInfo.setCreateTime(DateUtils.getNowDate());
        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改用户列表
     *
     * @param memberInfo 用户列表
     * @return 结果
     */
    @Override
    public int updateMemberInfo(MemberInfo memberInfo) {
        memberInfo.setUpdateTime(DateUtils.getNowDate());
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }

    @Override
    public int updateExtByMobile(String mobile) {
        memberInfoMapper.updateExtByMobile(mobile);
        memberExtInfoMapper.deleteMemberExtInfoByMobile(mobile);
        return 1;
    }

    /**
     * 批量删除用户列表
     *
     * @param uids 需要删除的用户列表主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByUids(String uids) {
        return memberInfoMapper.deleteMemberInfoByUids(Convert.toStrArray(uids));
    }

    /**
     * 删除用户列表信息
     *
     * @param uid 用户列表主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByUid(String uid) {
        return memberInfoMapper.deleteMemberInfoByUid(uid);
    }

    @Override
    public MemberInfo selectMemberInfoByMobile(String mobile) {
        return memberInfoMapper.selectMemberInfoByMobile(mobile);
    }

    @Override
    public List<Map<String, Object>> selectPushResultList(Long uid) {
        return memberInfoMapper.selectPushResultList(uid);
    }

    @Override
    public List<Map<String, Object>> getDataSortZhimaInfo(Integer type) {

        LocalDate localDate = LocalDate.now();

        if (type == 1) {
            // 昨天
            localDate = localDate.minusDays(1);
        } else if (type == 2) {
            // 近三天
            localDate = localDate.minusDays(2);
        } else if (type == 3) {
            // 近五天
            localDate = localDate.minusDays(4);
        }
        // 根据条件查询 全国的数据

        String dateInfo = localDate.toString();

        long timeMillis = System.currentTimeMillis();
        Long number = memberInfoMapper.countAuthNumber(localDate.toString());
//        System.out.println("================1111");
//        System.out.println(System.currentTimeMillis() - timeMillis);
//        System.out.println("================");

        List<Map<String, Object>> dataZhimaMapList = memberInfoMapper.getDataSortZhimaInfoByDateInfoAndChannelCode(dateInfo, null);

        Long zhima700Num = 0L;
        Long zhima650Num = 0L;
        Long zhima600Num = 0L;
        Long zhima001Num = 0L;
        Long zhima000Num = 0L;

        for (Map<String, Object> map : dataZhimaMapList) {
            String zhima = (String) map.get("zhima");
            Long itemNumber = (Long) map.get("number");
            if (StrUtil.equals(zhima, "zm005")){
                zhima700Num = itemNumber;
            }else if (StrUtil.equals(zhima, "zm004")){
                zhima650Num = itemNumber;
            }else if (StrUtil.equals(zhima, "zm003")){
                zhima600Num = itemNumber;
            }else if (StrUtil.equals(zhima, "zm002")){
                zhima001Num = itemNumber;
            }else if (StrUtil.equals(zhima, "zm001")){
                zhima000Num = itemNumber;
            }
        }

//        System.out.println("================");
//        System.out.println(System.currentTimeMillis() - timeMillis);
//        System.out.println("================");

        Map<String, Object> zhima700Map = new HashMap<>();
        zhima700Map.put("name", "芝麻700以上");
        zhima700Map.put("number", zhima700Num);
        zhima700Map.put("rate", String.valueOf(SysNumberUtil.getPointNumber(new BigDecimal(zhima700Num), new BigDecimal(number), 4)));

        Map<String, Object> zhima650Map = new HashMap<>();
        zhima650Map.put("name", "芝麻650-700");
        zhima650Map.put("number", zhima650Num);
        zhima650Map.put("rate", String.valueOf(SysNumberUtil.getPointNumber(new BigDecimal(zhima650Num), new BigDecimal(number), 4)));

        Map<String, Object> zhima600Map = new HashMap<>();
        zhima600Map.put("name", "芝麻600-650");
        zhima600Map.put("number", zhima600Num);
        zhima600Map.put("rate", String.valueOf(SysNumberUtil.getPointNumber(new BigDecimal(zhima600Num), new BigDecimal(number), 4)));

        Map<String, Object> zhima001Map = new HashMap<>();
        zhima001Map.put("name", "芝麻1-600");
        zhima001Map.put("number", zhima001Num);
        zhima001Map.put("rate", String.valueOf(SysNumberUtil.getPointNumber(new BigDecimal(zhima001Num), new BigDecimal(number), 4)));

        Map<String, Object> zhima000Map = new HashMap<>();
        zhima000Map.put("name", "无芝麻");
        zhima000Map.put("number", zhima000Num);
        zhima000Map.put("rate", String.valueOf(SysNumberUtil.getPointNumber(new BigDecimal(zhima000Num), new BigDecimal(number), 4)));



        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> totalMap = new HashMap<>();
        totalMap.put("name", "全国");
        totalMap.put("number", number);
        totalMap.put("rate", 100);
        resultList.add(totalMap);

        resultList.add(zhima700Map);
        resultList.add(zhima650Map);
        resultList.add(zhima600Map);
        resultList.add(zhima001Map);
        resultList.add(zhima000Map);


        return resultList;
    }

    @Override
    public List<Map<String, Object>> getDataSortZhimaInfoByDateInfoAndChannelCode(String dateInfo, String channelCode){
        return this.memberInfoMapper.getDataSortZhimaInfoByDateInfoAndChannelCode(dateInfo, channelCode);
    }
}
